package com.mysiteforme.admin.controller.email;

import com.xiaoleilu.hutool.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mysiteforme.admin.entity.EmailGroup;
import com.mysiteforme.admin.service.email.EmailGroupService;
import com.mysiteforme.admin.service.email.EmailService;
import com.mysiteforme.admin.util.other.LayerData;
import com.mysiteforme.admin.util.other.RestResponse;
import com.baomidou.mybatisplus.plugins.Page;
import com.mysiteforme.admin.annotation.SysLog;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 邮箱组  前端控制器
 * </p>
 */
@Controller
@RequestMapping("/admin/emailGroup")
public class EmailGroupController {
    private static final Logger LOGGER = LoggerFactory.getLogger(EmailGroupController.class);
    @Autowired
    private EmailService emailService;
    @Autowired
    private EmailGroupService emailGroupService;

    @GetMapping("list")
    @SysLog("跳转邮箱组列表")
    public String list(){
        return "/admin/emailGroup/list";
    }
    @RequiresPermissions("emailGroup:list")
    @PostMapping("list")
    @ResponseBody
    @SysLog("请求邮箱组列表数据")
    public LayerData<EmailGroup> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                      ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<EmailGroup> layerData = new LayerData<>();
        EntityWrapper<EmailGroup> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag",false);
        if(!map.isEmpty()){
            String emailTeam = (String) map.get("emailTeam");
            if(StringUtils.isNotBlank(emailTeam)) {
                wrapper.like("email_team",emailTeam);
            }else{
                map.remove("emailTeam");
            }

        }
        Page<EmailGroup> pageData = emailGroupService.selectPage(new Page<>(page,limit),wrapper);
        layerData.setData(pageData.getRecords());
        layerData.setCount(pageData.getTotal());
        return layerData;
    }

    @GetMapping("add")
    @SysLog("跳转新增邮箱组页面")
    public String add(Model model){
       model.addAttribute("email_group",emailService.selectList(null));
        return "/admin/emailGroup/add";
    }
    
    
    
    @RequiresPermissions("emailGroup:add")
    @PostMapping("add")
    @SysLog("保存新增邮箱组数据")
    @ResponseBody
    public RestResponse add(String chk_value,String emailTeam){
    	EmailGroup emailGroup = new EmailGroup();
    	emailGroup.setEmailTeam(emailTeam);
    	emailGroup.setEmailTeamAddress(chk_value);
        emailGroupService.insert(emailGroup);
        return RestResponse.success();
    }

    @GetMapping("edit")
    @SysLog("跳转编辑邮箱组页面")
    public String edit(Long id,Model model){
        EmailGroup emailGroup = emailGroupService.selectById(id);
        model.addAttribute("emailGroup",emailGroup);
        return "/admin/emailGroup/edit";
    }
    @RequiresPermissions("emailGroup:edit")
    @PostMapping("edit")
    @ResponseBody
    @SysLog("保存编辑邮箱组数据")
    public RestResponse edit(EmailGroup emailGroup){
        if(null == emailGroup.getId() || 0 == emailGroup.getId()){
            return RestResponse.failure("ID不能为空");
        }
        emailGroupService.updateById(emailGroup);
        return RestResponse.success();
    }
    @RequiresPermissions("emailGroup:delete")
    @PostMapping("delete")
    @ResponseBody
    @SysLog("删除邮箱组数据")
    public RestResponse delete(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        EmailGroup emailGroup = emailGroupService.selectById(id);
        emailGroup.setDelFlag(true);
        emailGroupService.updateById(emailGroup);
        return RestResponse.success();
    }

}